package com.fenghuajueli.idiomppp.ui.idiompin;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.adapter.IdiomSelectPassAdapter;
import com.fenghuajueli.idiomppp.base.JumpActivityUtils;
import com.fenghuajueli.idiomppp.databinding.ActivityIdiomSelectPassBinding;
import com.fenghuajueli.idiomppp.entity.GuessIdiomEntity;
import com.fenghuajueli.idiomppp.entity.IdiomPinParentEntity;
import com.fenghuajueli.idiomppp.entity.IdiomXiaoDbEntity;
import com.fenghuajueli.idiomppp.utils.MmkvKey;
import com.fenghuajueli.idiomppp.utils.RuntimeDataUtils;
import com.fenghuajueli.libbasecoreui.mvp.LifecycleBaseFragment;
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils;

import java.util.List;


public class ChengYuFragment extends LifecycleBaseFragment<IdiomSelectPassPresenter> implements IdiomSelectPassView {



    RelativeLayout containerView;
    GridView gridView;
    ImageView ivBtnUp;
    ImageView ivBtnNext;
    //    @BindView(R2.id.tv_title)
//    TextView tvTitle;
    IdiomSelectPassAdapter idiomSelectPassAdapter;
    private int passLevel = 0;
    private Boolean isFirstLoad = false;

    private ActivityIdiomSelectPassBinding binding;

    @Override
    public IdiomSelectPassPresenter initPresenter() {
        return new IdiomSelectPassPresenter(requireContext());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = ActivityIdiomSelectPassBinding.inflate(getLayoutInflater());
        containerView = binding.containerView;
        gridView = binding.gridView;
        ivBtnUp = binding.ivBtnUp;
        ivBtnNext = binding.ivBtnNext;
        OnClick(binding.getRoot());
        return binding.getRoot();
    }

    @Override
    public void initView(Bundle savedInstanceState) {
//        ImageLoadManager.loadForViewGroup(containerView, R.mipmap.aa_szxl_bj);
        passLevel = MmkvUtils.mmkv.decodeInt(MmkvKey.IdiomPinPass.name(), 0);
        presenter.initData();
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_idiom_select_pass;
    }

    @Override
    public void loadSuccess(List<IdiomPinParentEntity> idiomPinParentEntityList) {
        if (idiomSelectPassAdapter == null) {
            idiomSelectPassAdapter = new IdiomSelectPassAdapter(requireContext(), idiomPinParentEntityList);
            idiomSelectPassAdapter.setPassLevel(passLevel);
            gridView.setAdapter(idiomSelectPassAdapter);
            idiomSelectPassAdapter.setOnBaseClick((position, data) -> {
                RuntimeDataUtils.idiomPinParentEntity = data;
                JumpActivityUtils.goNormalActivity(requireContext(), IdiomPinPlayActivity.class, false);
            });
        } else {
            gridView.setAdapter(idiomSelectPassAdapter);
            passLevel = MmkvUtils.mmkv.decodeInt(MmkvKey.IdiomPinPass.name(), 0);
            idiomSelectPassAdapter.setPassLevel(passLevel);
            idiomSelectPassAdapter.refreshData(idiomPinParentEntityList);
        }
    }

    @Override
    public void changeBtnUpView(int viewStatus) {
        ivBtnUp.setVisibility(viewStatus);
    }

    @Override
    public void changeBtnNextView(int viewStatus) {
        ivBtnNext.setVisibility(viewStatus);
    }

    @Override
    public void loadXiaoDataSuccess(List<IdiomXiaoDbEntity> idiomPinParentEntityList) {

    }

    @Override
    public void loadGuessDataSuccess(List<GuessIdiomEntity> idiomPinParentEntityList) {

    }


    @Override
    public void onResume() {
        super.onResume();
        if (!isFirstLoad) {
            isFirstLoad = true;
        } else {
            passLevel = MmkvUtils.mmkv.decodeInt(MmkvKey.IdiomPinPass.name(), 0);
            idiomSelectPassAdapter.setPassLevel(passLevel);
            idiomSelectPassAdapter.notifyDataSetChanged();
        }
    }

    public void OnClick(View view) {
//        if (!QuickClickUtils.isFastClick(200)) {
//            return;
//        }
        int id = view.getId();
        if (id==R.id.ivBack){
            getActivity().onBackPressed();
        }
        if (id == R.id.ivBtnUp) {
            presenter.getUpPageData();
        } else if (id == R.id.ivBtnNext) {
            presenter.getNextPageData();
        }
    }
}
